/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.threadtask;

import com.alibaba.fastjson.JSONObject;
import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.DateUtils;
import com.xlx.openLink.conf.MailBean;
import com.xlx.openLink.conf.SystemInit;
import com.xlx.openLink.dao.AlarmMapper;
import com.xlx.openLink.entity.pojo.Alarm;
import com.xlx.openLink.entity.pojo.Inform;
import com.xlx.openLink.entity.pojo.Sys;
import com.xlx.openLink.enums.AlSendTypeEnum;
import com.xlx.openLink.enums.SysParamsEnums;
import com.xlx.openLink.service.AlarmService;
import com.xlx.openLink.service.InformService;
import com.xlx.openLink.service.ManagerService;
import com.xlx.openLink.service.SysService;
import com.xlx.openLink.uitls.Commuitls;
import com.xlx.openLink.uitls.SendEmailUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Slf4j
public class SendMsgTask {

    @Autowired
    private AlarmService alarmService;

    @Autowired
    private AlarmMapper alarmMapper;

    @Autowired
    private ManagerService managerService;
    @Autowired
    private InformService informService;
    @Autowired
    private SysService sysService;
    @Autowired
    JavaMailSender jms;

    //@Scheduled(fixedDelay = 60 * 1000)
    @Transactional
    public void sendEmail() {
        List<Alarm> alarmList = alarmService.selectNoSend();
        List<Inform> informs = informService.selectValid();
        alarmList.forEach(alarm -> {
            //message
            informs.forEach(inform -> {
                if ("E".equals(inform.getiType()) && StringUtils.isNotEmpty(inform.getiAccount())) {
                    try {
                        String message = alarm.getAlMsg() + "\n" + "告警产生时间:" + DateUtils.formatDateTime(alarm.getAlCreateDate());
                        Sys sys = sysService.selctByCode(SysParamsEnums.SYS_MAIL.getCode());
                        if (sys != null && sys.getSysParam() != null) {
                            log.info("配置邮件....");
                            SendEmailUtils.sendMaill(JSONObject.parseObject(sys.getSysParam(), MailBean.class), inform.getiAccount(), message);//发送邮件
                        } else {
                            Commuitls.sendEmail(jms, inform.getiAccount(), alarm.getAlMsg(), DateUtils.formatDateTime(alarm.getAlCreateDate()));
                        }
                    } catch (Exception e) {
                        log.info("配置邮件发送失败，默认邮箱开始发送");
                        Commuitls.sendEmail(jms, inform.getiAccount(), alarm.getAlMsg(), DateUtils.formatDateTime(alarm.getAlCreateDate()));
                    }
                }
                if ("P".equals(inform.getiType()) && StringUtils.isNotEmpty(inform.getiAccount())) {
                    String message = alarm.getAlMsg() + "\n" + "告警产生时间:" + DateUtils.formatDateTime(alarm.getAlCreateDate());
                    BaseUtils.sendSms(inform.getiAccount(), message);//发送短信
                }
            });
            alarm.setAlSendStatus(AlSendTypeEnum.SENT.getCode());
        });
        alarmService.updateBatch(alarmList);
    }


}
